<template>
  <div class="conent">
    <img
      src="https://ts1.cn.mm.bing.net/th?id=ORMS.170e336c216afedb6ef21d28113833be&pid=Wdp&w=300&h=156&qlt=90&c=1&rs=1&dpr=0.8999999761581421&p=0"
    />
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="username">
        <el-input v-model="ruleForm.username" type="text" />
      </el-form-item>
      <el-form-item label="账号" prop="password">
        <el-input v-model="ruleForm.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script  setup lang="ts" >
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import  {loginpm}  from '../api/login'
import router from '@/router'
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  username: '',
  password: ''
})
const validatePass = (rule: any, value: any, callback: any) => {
     const rexp=/^\d{5,8}$/
     if(rexp.test(value)){
        callback()
     }else{
        callback(new Error('请输入5-8位数字'))
     }
}
const rules = reactive<FormRules<typeof ruleForm>>({
  username: [{trigger: 'blur',required:true,message:'请输入账号' }],
  password: [{trigger: 'blur',required:true,message:'请输入密码'},{
    validator:validatePass
  }
]
})
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(async(valid: any) => {
    if (valid) {
      const res=await loginpm(ruleForm)
       console.log(res.data,'submit!')
      if(res.data.code===200){
        localStorage.setItem('token',res.data.token)
        localStorage.setItem('obj',JSON.stringify(res.data.obj))
         router.push('/home')
      }
     
    } else {
      console.log('error submit!')
      return false
    }
  })
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
<style scoped>
.conent {
  width: 100%;
  height: 100%;
}
img {
  position: absolute;
  width: 100%;
  height: 100%;
}
.demo-ruleForm {
  position: absolute;
  top: 35%;
  left: 45%;
}
</style>
